package cn.fintecher.pangolin.service.dataimp.model.request;

import cn.fintecher.pangolin.common.enums.*;
import cn.fintecher.pangolin.entity.mysql.QCaseInfo;
import com.querydsl.core.BooleanBuilder;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @Auther: xiaqun
 * @Description: 导出案件请求参数
 * @Date: 13:51 2019/10/19
 */

@Data
public class ExportCaseRequest {
    @ApiModelProperty(value = "案件池类型")
    private String type;

    @ApiModelProperty(value = "委托方ID")
    private String principalId;

    @ApiModelProperty(value = "批次号")
    private String batchNumber;

    @ApiModelProperty(value = "最小欠款金额")
    private Double minLeftAmt;

    @ApiModelProperty(value = "最大欠款金额")
    private Double maxLeftAmt;

    @ApiModelProperty(value = "委案城市")
    private String cities;

    @ApiModelProperty(value = "证件号")
    private String certificateNos;

    @ApiModelProperty(value = "账号")
    private String account;

    @ApiModelProperty(value = "卡号")
    private String cardNo;

    @ApiModelProperty(value = "是否结清")
    private PayStatus payStatus;

    @ApiModelProperty(value = "委案阶段")
    private String stage;

    @ApiModelProperty(value = "模板ID")
    private String templateId;

    public BoolQueryBuilder getESBuilder() {
        BoolQueryBuilder builder = new BoolQueryBuilder();
        switch (this.type) {
            case "WAIT_DISTRIBUTE": //待分配
                builder.must(QueryBuilders.matchPhraseQuery("isDistribute.keyword", BaseSelect.NO.name()));
                break;
            case "RETURN": //退案
                builder.must(QueryBuilders.matchPhraseQuery("historyCaseStatus.keyword", HistoryCaseStatus.RETIRE_POOL.name()));
                break;
            case "DELETE": //删除
                builder.must(QueryBuilders.matchPhraseQuery("historyCaseStatus.keyword", HistoryCaseStatus.DELETE_POOL.name()));
                break;
        }
        if (Objects.nonNull(this.principalId)) {
            builder.must(QueryBuilders.matchPhraseQuery("principalId.keyword", this.principalId));
        }
        if (StringUtils.isNotBlank(this.batchNumber)) {
            builder.must(QueryBuilders.matchPhraseQuery("batchNumber.keyword", this.batchNumber));
        }
        if (Objects.nonNull(this.minLeftAmt)) {
            builder.must(QueryBuilders.rangeQuery("leftAmt").gte(this.minLeftAmt));
        }
        if (Objects.nonNull(this.maxLeftAmt)) {
            builder.must(QueryBuilders.rangeQuery("leftAmt").lte(this.maxLeftAmt));
        }
        if (StringUtils.isNotBlank(this.cities)) {
            List<String> city = Arrays.asList(this.cities.split(","));
            city.forEach(e -> {
                if (e.equals("")) {
                    city.remove(e);
                }
            });
            builder.must(QueryBuilders.termsQuery("city.keyword", city));
        }
        if (StringUtils.isNotBlank(this.certificateNos)) {
            List<String> certificateNo = Arrays.asList(this.certificateNos.split(","));
            certificateNo.forEach(e -> {
                if (e.equals("")) {
                    certificateNo.remove(e);
                }
            });
            builder.must(QueryBuilders.termsQuery("certificateNo.keyword", certificateNo));
        }
        if (StringUtils.isNotBlank(this.account)) {
            builder.must(QueryBuilders.matchPhraseQuery("account.keyword", this.account));
        }
        if (StringUtils.isNotBlank(this.cardNo)) {
            builder.must(QueryBuilders.matchPhraseQuery("cardNo.keyword", this.cardNo));
        }
        if (StringUtils.isNotBlank(this.stage)) {
            builder.must(QueryBuilders.matchPhraseQuery("stage.keyword", this.stage));
        }
        if (Objects.nonNull(this.payStatus)) {
            builder.must(QueryBuilders.matchPhraseQuery("payStatus.keyword", this.payStatus.name()));
        }
        return builder;
    }

    public BooleanBuilder getSqlBuilder() {
        BooleanBuilder builder = new BooleanBuilder();
        QCaseInfo qCaseInfo = QCaseInfo.caseInfo;
        switch (this.type) {
            case "DEPARTMENT": //机构待分配
                builder.and(qCaseInfo.currentCollector.isNull()).and(qCaseInfo.department.isNotNull()).and(qCaseInfo.caseDataStatus.ne(CaseDataStatus.PAUSE));
                break;
            case "COLLECTING": //在催
                builder.and(qCaseInfo.currentCollector.isNotNull()).and(qCaseInfo.department.isNull()).and(qCaseInfo.caseDataStatus.ne(CaseDataStatus.PAUSE));
                break;
            case "PUBLIC": //公共
                builder.and(qCaseInfo.publicCaseFlag.eq(PublicCaseFlag.PUBLIC_CASE));
                break;
            case "STOPPING": //停催
                builder.and(qCaseInfo.caseDataStatus.eq(CaseDataStatus.PAUSE));
                break;
            case "CIRCULATION": //流转
                builder.and(qCaseInfo.caseDataStatus.eq(CaseDataStatus.CIRCULATION));
                break;
        }
        if (Objects.nonNull(this.principalId)) {
            builder.and(qCaseInfo.principalId.eq(this.principalId));
        }
        if (StringUtils.isNotBlank(this.batchNumber)) {
            builder.and(qCaseInfo.batchNumber.eq(this.batchNumber));
        }
        if (Objects.nonNull(this.minLeftAmt)) {
            builder.and(qCaseInfo.leftAmt.goe(this.minLeftAmt));
        }
        if (Objects.nonNull(this.maxLeftAmt)) {
            builder.and(qCaseInfo.leftAmt.loe(this.maxLeftAmt));
        }
        if (StringUtils.isNotBlank(this.cities)) {
            List<String> city = Arrays.asList(this.cities.split(","));
            city.forEach(e -> {
                if (e.equals("")) {
                    city.remove(e);
                }
            });
            builder.and(qCaseInfo.city.in(city));
        }
        if (StringUtils.isNotBlank(this.certificateNos)) {
            List<String> certificateNo = Arrays.asList(this.certificateNos.split(","));
            certificateNo.forEach(e -> {
                if (e.equals("")) {
                    certificateNo.remove(e);
                }
            });
            builder.and(qCaseInfo.certificateNo.in(certificateNo));
        }
        if (StringUtils.isNotBlank(this.account)) {
            builder.and(qCaseInfo.account.eq(this.account));
        }
        if (StringUtils.isNotBlank(this.cardNo)) {
            builder.and(qCaseInfo.cardNo.eq(this.cardNo));
        }
        if (StringUtils.isNotBlank(this.stage)) {
            builder.and(qCaseInfo.stage.eq(this.stage));
        }
        if (Objects.nonNull(this.payStatus)) {
            builder.and(qCaseInfo.payStatus.eq(this.payStatus));
        }
        return builder;
    }
}